home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / reuse.lha / reuse / m2c / Relations.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  18KB  |  922 lines

  1. #include "SYSTEM_.h"
  2.  
  3. #ifndef DEFINITION_IO
  4. #include "IO.h"
  5. #endif
  6.  
  7. #ifndef DEFINITION_DynArray
  8. #include "DynArray.h"
  9. #endif
  10.  
  11. #ifndef DEFINITION_Sets
  12. #include "Sets.h"
  13. #endif
  14.  
  15. #ifndef DEFINITION_Sets
  16. #include "Sets.h"
  17. #endif
  18.  
  19. #ifndef DEFINITION_Relations
  20. #include "Relations.h"
  21. #endif
  22.  
  23.  
  24. static SHORTCARD i, j;
  25. static Relations_tRelation gRel;
  26. static BOOLEAN gSymmetric ARGS((CARDINAL e));
  27. typedef struct S_1 {
  28.     SHORTCARD A[100000000 + 1];
  29. } PredCount;
  30. static PredCount *PredCountPtr;
  31. static Sets_tSet WithoutPred;
  32. static void gPredCount ARGS((CARDINAL e));
  33. static void gPredCount2 ARGS((CARDINAL e));
  34. static Relations_ProcOfIntIntToBool gProc2b;
  35. static BOOLEAN gProc1b ARGS((CARDINAL e));
  36. static Relations_ProcOfIntInt gProc2;
  37. static void gProc1 ARGS((CARDINAL e));
  38. static IO_tFile g;
  39. static void WritePair ARGS((INTEGER e1, INTEGER e2));
  40.  
  41.  
  42. void Relations_MakeRelation
  43. # ifdef __STDC__
  44. (Relations_tRelation *Rel, INTEGER Size1, INTEGER Size2)
  45. # else
  46. (Rel, Size1, Size2)
  47. Relations_tRelation *Rel;
  48. INTEGER Size1, Size2;
  49. # endif
  50. {
  51.   LONGINT ElmtCount;
  52.  
  53.   Rel->Size1 = Size1;
  54.   Rel->Size2 = Size2;
  55.   ElmtCount = Size1 + 1;
  56.   DynArray_MakeArray((ADDRESS *)&Rel->ArrayPtr, &ElmtCount, (LONGINT)sizeof(Sets_tSet));
  57.   {
  58.     SHORTCARD B_1 = 0, B_2 = Rel->Size1;
  59.  
  60.     if (B_1 <= B_2)
  61.       for (i = B_1;; i += 1) {
  62.         Sets_MakeSet(&Rel->ArrayPtr->A[i], (LONGCARD)Size2);
  63.         if (i >= B_2) break;
  64.       }
  65.   }
  66. }
  67.  
  68. void Relations_ReleaseRelation
  69. # ifdef __STDC__
  70. (Relations_tRelation *Rel)
  71. # else
  72. (Rel)
  73. Relations_tRelation *Rel;
  74. # endif
  75. {
  76.   LONGINT ElmtCount;
  77.  
  78.   {
  79.     SHORTCARD B_3 = 0, B_4 = Rel->Size1;
  80.  
  81.     if (B_3 <= B_4)
  82.       for (i = B_3;; i += 1) {
  83.         Sets_ReleaseSet(&Rel->ArrayPtr->A[i]);
  84.         if (i >= B_4) break;
  85.       }
  86.   }
  87.   ElmtCount = Rel->Size1 + 1;
  88.   DynArray_ReleaseArray((ADDRESS *)&Rel->ArrayPtr, &ElmtCount, (LONGINT)sizeof(Sets_tSet));
  89. }
  90.  
  91. void Relations_Include
  92. # ifdef __STDC__
  93. (Relations_tRelation *Rel, INTEGER e1, INTEGER e2)
  94. # else
  95. (Rel, e1, e2)
  96. Relations_tRelation *Rel;
  97. INTEGER e1, e2;
  98. # endif
  99. {
  100.   Sets_Include(&Rel->ArrayPtr->A[e1], (LONGCARD)e2);
  101. }
  102.  
  103. void Relations_Exclude
  104. # ifdef __STDC__
  105. (Relations_tRelation *Rel, INTEGER e1, INTEGER e2)
  106. # else
  107. (Rel, e1, e2)
  108. Relations_tRelation *Rel;
  109. INTEGER e1, e2;
  110. # endif
  111. {
  112.   Sets_Exclude(&Rel->ArrayPtr->A[e1], (LONGCARD)e2);
  113. }
  114.  
  115. BOOLEAN Relations_IsElement
  116. # ifdef __STDC__
  117. (INTEGER e1, INTEGER e2, Relations_tRelation Rel)
  118. # else
  119. (e1, e2, Rel)
  120. INTEGER e1, e2;
  121. Relations_tRelation Rel;
  122. # endif
  123. {
  124.   return Sets_IsElement((LONGCARD)e2, &Rel.ArrayPtr->A[e1]);
  125. }
  126.  
  127. BOOLEAN Relations_IsRelated
  128. # ifdef __STDC__
  129. (INTEGER e1, INTEGER e2, Relations_tRelation Rel)
  130. # else
  131. (e1, e2, Rel)
  132. INTEGER e1, e2;
  133. Relations_tRelation Rel;
  134. # endif
  135. {
  136.   return Sets_IsElement((LONGCARD)e2, &Rel.ArrayPtr->A[e1]);
  137. }
  138.  
  139. BOOLEAN Relations_IsReflexive1
  140. # ifdef __STDC__
  141. (INTEGER e1, Relations_tRelation Rel)
  142. # else
  143. (e1, Rel)
  144. INTEGER e1;
  145. Relations_tRelation Rel;
  146. # endif
  147. {
  148.   return Sets_IsElement((LONGCARD)e1, &Rel.ArrayPtr->A[e1]);
  149. }
  150.  
  151. BOOLEAN Relations_IsSymmetric1
  152. # ifdef __STDC__
  153. (INTEGER e1, INTEGER e2, Relations_tRelation Rel)
  154. # else
  155. (e1, e2, Rel)
  156. INTEGER e1, e2;
  157. Relations_tRelation Rel;
  158. # endif
  159. {
  160.   return !Sets_IsElement((LONGCARD)e2, &Rel.ArrayPtr->A[e1]) || Sets_IsElement((LONGCARD)e1, &Rel.ArrayPtr->A[e2]);
  161. }
  162.  
  163. BOOLEAN Relations_IsTransitive1
  164. # ifdef __STDC__
  165. (INTEGER e1, INTEGER e2, INTEGER e3, Relations_tRelation Rel)
  166. # else
  167. (e1, e2, e3, Rel)
  168. INTEGER e1, e2, e3;
  169. Relations_tRelation Rel;
  170. # endif
  171. {
  172.   return !(Sets_IsElement((LONGCARD)e2, &Rel.ArrayPtr->A[e1]) && Sets_IsElement((LONGCARD)e3, &Rel.ArrayPtr->A[e2])) || Sets_IsElement((LONGCARD)e3, &Rel.ArrayPtr->A[e1]);
  173. }
  174.  
  175. BOOLEAN Relations_IsReflexive
  176. # ifdef __STDC__
  177. (Relations_tRelation Rel)
  178. # else
  179. (Rel)
  180. Relations_tRelation Rel;
  181. # endif
  182. {
  183.   {
  184.     SHORTCARD B_5 = 0, B_6 = Rel.Size1;
  185.  
  186.     if (B_5 <= B_6)
  187.       for (i = B_5;; i += 1) {
  188.         if (!Sets_IsElement((LONGCARD)i, &Rel.ArrayPtr->A[i])) {
  189.           return FALSE;
  190.         }
  191.         if (i >= B_6) break;
  192.       }
  193.   }
  194.   return TRUE;
  195. }
  196.  
  197. static BOOLEAN gSymmetric
  198. # ifdef __STDC__
  199. (CARDINAL e)
  200. # else
  201. (e)
  202. CARDINAL e;
  203. # endif
  204. {
  205.   return Sets_IsElement((LONGCARD)i, &gRel.ArrayPtr->A[e]);
  206. }
  207.  
  208. BOOLEAN Relations_IsSymmetric
  209. # ifdef __STDC__
  210. (Relations_tRelation Rel)
  211. # else
  212. (Rel)
  213. Relations_tRelation Rel;
  214. # endif
  215. {
  216.   gRel = Rel;
  217.   {
  218.     SHORTCARD B_7 = 0, B_8 = Rel.Size1;
  219.  
  220.     if (B_7 <= B_8)
  221.       for (i = B_7;; i += 1) {
  222.         if (!Sets_Forall(Rel.ArrayPtr->A[i], (Sets_ProcOfCardToBool)gSymmetric)) {
  223.           return FALSE;
  224.         }
  225.         if (i >= B_8) break;
  226.       }
  227.   }
  228.   return TRUE;
  229. }
  230.  
  231. BOOLEAN Relations_IsTransitive
  232. # ifdef __STDC__
  233. (Relations_tRelation Rel)
  234. # else
  235. (Rel)
  236. Relations_tRelation Rel;
  237. # endif
  238. {
  239.   Relations_tRelation r;
  240.   BOOLEAN Result;
  241.  
  242.   Relations_MakeRelation(&r, (LONGINT)Rel.Size1, (LONGINT)Rel.Size2);
  243.   Relations_Assign(&r, Rel);
  244.   Relations_Closure(&r);
  245.   Result = Relations_IsEqual(&r, &Rel);
  246.   Relations_ReleaseRelation(&r);
  247.   return Result;
  248. }
  249.  
  250. BOOLEAN Relations_IsEquivalence
  251. # ifdef __STDC__
  252. (Relations_tRelation Rel)
  253. # else
  254. (Rel)
  255. Relations_tRelation Rel;
  256. # endif
  257. {
  258.   return Relations_IsReflexive(Rel) && Relations_IsSymmetric(Rel) && Relations_IsTransitive(Rel);
  259. }
  260.  
  261. BOOLEAN Relations_HasReflexive
  262. # ifdef __STDC__
  263. (Relations_tRelation Rel)
  264. # else
  265. (Rel)
  266. Relations_tRelation Rel;
  267. # endif
  268. {
  269.   {
  270.     SHORTCARD B_9 = 0, B_10 = Rel.Size1;
  271.  
  272.     if (B_9 <= B_10)
  273.       for (i = B_9;; i += 1) {
  274.         if (Sets_IsElement((LONGCARD)i, &Rel.ArrayPtr->A[i])) {
  275.           return TRUE;
  276.         }
  277.         if (i >= B_10) break;
  278.       }
  279.   }
  280.   return FALSE;
  281. }
  282.  
  283. BOOLEAN Relations_IsCyclic
  284. # ifdef __STDC__
  285. (Relations_tRelation Rel)
  286. # else
  287. (Rel)
  288. Relations_tRelation Rel;
  289. # endif
  290. {
  291.   LONGINT PredCountSize;
  292.   Sets_tSet WithPred;
  293.   BOOLEAN Result;
  294.  
  295.   PredCountSize = Rel.Size1 + 1;
  296.   DynArray_MakeArray((ADDRESS *)&PredCountPtr, &PredCountSize, (LONGINT)sizeof(SHORTCARD));
  297.   Sets_MakeSet(&WithoutPred, (LONGCARD)Rel.Size1);
  298.   Sets_MakeSet(&WithPred, (LONGCARD)Rel.Size1);
  299.   {
  300.     SHORTCARD B_11 = 0, B_12 = Rel.Size1;
  301.  
  302.     if (B_11 <= B_12)
  303.       for (i = B_11;; i += 1) {
  304.         PredCountPtr->A[i] = 0;
  305.         if (i >= B_12) break;
  306.       }
  307.   }
  308.   {
  309.     SHORTCARD B_13 = 0, B_14 = Rel.Size1;
  310.  
  311.     if (B_13 <= B_14)
  312.       for (i = B_13;; i += 1) {
  313.         Sets_ForallDo(Rel.ArrayPtr->A[i], (Sets_ProcOfCard)gPredCount);
  314.         if (i >= B_14) break;
  315.       }
  316.   }
  317.   {
  318.     SHORTCARD B_15 = 0, B_16 = Rel.Size1;
  319.  
  320.     if (B_15 <= B_16)
  321.       for (i = B_15;; i += 1) {
  322.         if (PredCountPtr->A[i] == 0) {
  323.           Sets_Include(&WithoutPred, (LONGCARD)i);
  324.         }
  325.         if (i >= B_16) break;
  326.       }
  327.   }
  328.   Sets_Complement(&WithPred);
  329.   while (!Sets_IsEmpty(WithoutPred)) {
  330.     i = Sets_Extract(&WithoutPred);
  331.     Sets_Exclude(&WithPred, (LONGCARD)i);
  332.     Sets_ForallDo(Rel.ArrayPtr->A[i], (Sets_ProcOfCard)gPredCount2);
  333.   }
  334.   Result = !Sets_IsEmpty(WithPred);
  335.   Sets_ReleaseSet(&WithoutPred);
  336.   Sets_ReleaseSet(&WithPred);
  337.   DynArray_ReleaseArray((ADDRESS *)&PredCountPtr, &PredCountSize, (LONGINT)sizeof(SHORTCARD));
  338.   return Result;
  339. }
  340.  
  341. static void gPredCount
  342. # ifdef __STDC__
  343. (CARDINAL e)
  344. # else
  345. (e)
  346. CARDINAL e;
  347. # endif
  348. {
  349.   INC(PredCountPtr->A[e]);
  350. }
  351.  
  352. static void gPredCount2
  353. # ifdef __STDC__
  354. (CARDINAL e)
  355. # else
  356. (e)
  357. CARDINAL e;
  358. # endif
  359. {
  360.   DEC(PredCountPtr->A[e]);
  361.   if (PredCountPtr->A[e] == 0) {
  362.     Sets_Include(&WithoutPred, e);
  363.   }
  364. }
  365.  
  366. void Relations_GetCyclics
  367. # ifdef __STDC__
  368. (Relations_tRelation Rel, Sets_tSet *Set)
  369. # else
  370. (Rel, Set)
  371. Relations_tRelation Rel;
  372. Sets_tSet *Set;
  373. # endif
  374. {
  375.   Relations_tRelation r;
  376.  
  377.   Relations_MakeRelation(&r, (LONGINT)Rel.Size1, (LONGINT)Rel.Size2);
  378.   Relations_Assign(&r, Rel);
  379.   Relations_Closure(&r);
  380.   Sets_AssignEmpty(Set);
  381.   {
  382.     SHORTCARD B_17 = 0, B_18 = r.Size1;
  383.  
  384.     if (B_17 <= B_18)
  385.       for (i = B_17;; i += 1) {
  386.         if (Sets_IsElement((LONGCARD)i, &r.ArrayPtr->A[i])) {
  387.           Sets_Include(Set, (LONGCARD)i);
  388.         }
  389.         if (i >= B_18) break;
  390.       }
  391.   }
  392.   Relations_ReleaseRelation(&r);
  393. }
  394.  
  395. void Relations_AssignEmpty
  396. # ifdef __STDC__
  397. (Relations_tRelation *Rel)
  398. # else
  399. (Rel)
  400. Relations_tRelation *Rel;
  401. # endif
  402. {
  403.   {
  404.     SHORTCARD B_19 = 0, B_20 = Rel->Size1;
  405.  
  406.     if (B_19 <= B_20)
  407.       for (i = B_19;; i += 1) {
  408.         Sets_AssignEmpty(&Rel->ArrayPtr->A[i]);
  409.         if (i >= B_20) break;
  410.       }
  411.   }
  412. }
  413.  
  414. void Relations_AssignElmt
  415. # ifdef __STDC__
  416. (Relations_tRelation *Rel, INTEGER e1, INTEGER e2)
  417. # else
  418. (Rel, e1, e2)
  419. Relations_tRelation *Rel;
  420. INTEGER e1, e2;
  421. # endif
  422. {
  423.   Relations_AssignEmpty(Rel);
  424.   Relations_Include(Rel, e1, e2);
  425. }
  426.  
  427. void Relations_Assign
  428. # ifdef __STDC__
  429. (Relations_tRelation *Rel1, Relations_tRelation Rel2)
  430. # else
  431. (Rel1, Rel2)
  432. Relations_tRelation *Rel1;
  433. Relations_tRelation Rel2;
  434. # endif
  435. {
  436.   {
  437.     SHORTCARD B_21 = 0, B_22 = Rel1->Size1;
  438.  
  439.     if (B_21 <= B_22)
  440.       for (i = B_21;; i += 1) {
  441.         Sets_Assign(&Rel1->ArrayPtr->A[i], Rel2.ArrayPtr->A[i]);
  442.         if (i >= B_22) break;
  443.       }
  444.   }
  445. }
  446.  
  447. void Relations_Closure
  448. # ifdef __STDC__
  449. (Relations_tRelation *Rel)
  450. # else
  451. (Rel)
  452. Relations_tRelation *Rel;
  453. # endif
  454. {
  455.   Sets_tSet aj;
  456.  
  457.   {
  458.     register Relations_tRelation *W_1 = Rel;
  459.  
  460.     {
  461.       SHORTCARD B_23 = 0, B_24 = W_1->Size1;
  462.  
  463.       if (B_23 <= B_24)
  464.         for (j = B_23;; j += 1) {
  465.           if (!Sets_IsEmpty(W_1->ArrayPtr->A[j])) {
  466.             aj = W_1->ArrayPtr->A[j];
  467.             {
  468.               SHORTCARD B_25 = 0, B_26 = W_1->Size1;
  469.  
  470.               if (B_25 <= B_26)
  471.                 for (i = B_25;; i += 1) {
  472.                   if (Sets_IsElement((LONGCARD)j, &W_1->ArrayPtr->A[i])) {
  473.                     Sets_Union(&W_1->ArrayPtr->A[i], aj);
  474.                   }
  475.                   if (i >= B_26) break;
  476.                 }
  477.             }
  478.           }
  479.           if (j >= B_24) break;
  480.         }
  481.     }
  482.   }
  483. }
  484.  
  485. void Relations_Union
  486. # ifdef __STDC__
  487. (Relations_tRelation *Rel1, Relations_tRelation Rel2)
  488. # else
  489. (Rel1, Rel2)
  490. Relations_tRelation *Rel1;
  491. Relations_tRelation Rel2;
  492. # endif
  493. {
  494.   {
  495.     SHORTCARD B_27 = 0, B_28 = Rel1->Size1;
  496.  
  497.     if (B_27 <= B_28)
  498.       for (i = B_27;; i += 1) {
  499.         Sets_Union(&Rel1->ArrayPtr->A[i], Rel2.ArrayPtr->A[i]);
  500.         if (i >= B_28) break;
  501.       }
  502.   }
  503. }
  504.  
  505. void Relations_Difference
  506. # ifdef __STDC__
  507. (Relations_tRelation *Rel1, Relations_tRelation Rel2)
  508. # else
  509. (Rel1, Rel2)
  510. Relations_tRelation *Rel1;
  511. Relations_tRelation Rel2;
  512. # endif
  513. {
  514.   {
  515.     SHORTCARD B_29 = 0, B_30 = Rel1->Size1;
  516.  
  517.     if (B_29 <= B_30)
  518.       for (i = B_29;; i += 1) {
  519.         Sets_Difference(&Rel1->ArrayPtr->A[i], Rel2.ArrayPtr->A[i]);
  520.         if (i >= B_30) break;
  521.       }
  522.   }
  523. }
  524.  
  525. void Relations_Intersection
  526. # ifdef __STDC__
  527. (Relations_tRelation *Rel1, Relations_tRelation Rel2)
  528. # else
  529. (Rel1, Rel2)
  530. Relations_tRelation *Rel1;
  531. Relations_tRelation Rel2;
  532. # endif
  533. {
  534.   {
  535.     SHORTCARD B_31 = 0, B_32 = Rel1->Size1;
  536.  
  537.     if (B_31 <= B_32)
  538.       for (i = B_31;; i += 1) {
  539.         Sets_Intersection(&Rel1->ArrayPtr->A[i], Rel2.ArrayPtr->A[i]);
  540.         if (i >= B_32) break;
  541.       }
  542.   }
  543. }
  544.  
  545. void Relations_SymDiff
  546. # ifdef __STDC__
  547. (Relations_tRelation *Rel1, Relations_tRelation Rel2)
  548. # else
  549. (Rel1, Rel2)
  550. Relations_tRelation *Rel1;
  551. Relations_tRelation Rel2;
  552. # endif
  553. {
  554.   {
  555.     SHORTCARD B_33 = 0, B_34 = Rel1->Size1;
  556.  
  557.     if (B_33 <= B_34)
  558.       for (i = B_33;; i += 1) {
  559.         Sets_SymDiff(&Rel1->ArrayPtr->A[i], Rel2.ArrayPtr->A[i]);
  560.         if (i >= B_34) break;
  561.       }
  562.   }
  563. }
  564.  
  565. void Relations_Complement
  566. # ifdef __STDC__
  567. (Relations_tRelation *Rel)
  568. # else
  569. (Rel)
  570. Relations_tRelation *Rel;
  571. # endif
  572. {
  573.   {
  574.     SHORTCARD B_35 = 0, B_36 = Rel->Size1;
  575.  
  576.     if (B_35 <= B_36)
  577.       for (i = B_35;; i += 1) {
  578.         Sets_Complement(&Rel->ArrayPtr->A[i]);
  579.         if (i >= B_36) break;
  580.       }
  581.   }
  582. }
  583.  
  584. BOOLEAN Relations_IsSubset
  585. # ifdef __STDC__
  586. (Relations_tRelation Rel1, Relations_tRelation Rel2)
  587. # else
  588. (Rel1, Rel2)
  589. Relations_tRelation Rel1, Rel2;
  590. # endif
  591. {
  592.   {
  593.     SHORTCARD B_37 = 0, B_38 = Rel1.Size1;
  594.  
  595.     if (B_37 <= B_38)
  596.       for (i = B_37;; i += 1) {
  597.         if (!Sets_IsSubset(Rel1.ArrayPtr->A[i], Rel2.ArrayPtr->A[i])) {
  598.           return FALSE;
  599.         }
  600.         if (i >= B_38) break;
  601.       }
  602.   }
  603.   return TRUE;
  604. }
  605.  
  606. BOOLEAN Relations_IsStrictSubset
  607. # ifdef __STDC__
  608. (Relations_tRelation Rel1, Relations_tRelation Rel2)
  609. # else
  610. (Rel1, Rel2)
  611. Relations_tRelation Rel1, Rel2;
  612. # endif
  613. {
  614.   return Relations_IsSubset(Rel1, Rel2) && Relations_IsNotEqual(Rel1, Rel2);
  615. }
  616.  
  617. BOOLEAN Relations_IsEqual
  618. # ifdef __STDC__
  619. (Relations_tRelation *Rel1, Relations_tRelation *Rel2)
  620. # else
  621. (Rel1, Rel2)
  622. Relations_tRelation *Rel1, *Rel2;
  623. # endif
  624. {
  625.   {
  626.     SHORTCARD B_39 = 0, B_40 = Rel1->Size1;
  627.  
  628.     if (B_39 <= B_40)
  629.       for (i = B_39;; i += 1) {
  630.         if (!Sets_IsEqual(&Rel1->ArrayPtr->A[i], &Rel2->ArrayPtr->A[i])) {
  631.           return FALSE;
  632.         }
  633.         if (i >= B_40) break;
  634.       }
  635.   }
  636.   return TRUE;
  637. }
  638.  
  639. BOOLEAN Relations_IsNotEqual
  640. # ifdef __STDC__
  641. (Relations_tRelation Rel1, Relations_tRelation Rel2)
  642. # else
  643. (Rel1, Rel2)
  644. Relations_tRelation Rel1, Rel2;
  645. # endif
  646. {
  647.   return !Relations_IsEqual(&Rel1, &Rel2);
  648. }
  649.  
  650. BOOLEAN Relations_IsEmpty
  651. # ifdef __STDC__
  652. (Relations_tRelation Rel)
  653. # else
  654. (Rel)
  655. Relations_tRelation Rel;
  656. # endif
  657. {
  658.   {
  659.     SHORTCARD B_41 = 0, B_42 = Rel.Size1;
  660.  
  661.     if (B_41 <= B_42)
  662.       for (i = B_41;; i += 1) {
  663.         if (!Sets_IsEmpty(Rel.ArrayPtr->A[i])) {
  664.           return FALSE;
  665.         }
  666.         if (i >= B_42) break;
  667.       }
  668.   }
  669.   return TRUE;
  670. }
  671.  
  672. INTEGER Relations_Card
  673. # ifdef __STDC__
  674. (Relations_tRelation *Rel)
  675. # else
  676. (Rel)
  677. Relations_tRelation *Rel;
  678. # endif
  679. {
  680.   INTEGER n;
  681.  
  682.   n = 0;
  683.   {
  684.     SHORTCARD B_43 = 0, B_44 = Rel->Size1;
  685.  
  686.     if (B_43 <= B_44)
  687.       for (i = B_43;; i += 1) {
  688.         INC1(n, Sets_Card(&Rel->ArrayPtr->A[i]));
  689.         if (i >= B_44) break;
  690.       }
  691.   }
  692.   return n;
  693. }
  694.  
  695. void Relations_Select
  696. # ifdef __STDC__
  697. (Relations_tRelation *Rel, INTEGER *e1, INTEGER *e2)
  698. # else
  699. (Rel, e1, e2)
  700. Relations_tRelation *Rel;
  701. INTEGER *e1, *e2;
  702. # endif
  703. {
  704.   {
  705.     SHORTCARD B_45 = 0, B_46 = Rel->Size1;
  706.  
  707.     if (B_45 <= B_46)
  708.       for (i = B_45;; i += 1) {
  709.         if (!Sets_IsEmpty(Rel->ArrayPtr->A[i])) {
  710.           *e1 = i;
  711.           *e2 = Sets_Select(&Rel->ArrayPtr->A[i]);
  712.           return;
  713.         }
  714.         if (i >= B_46) break;
  715.       }
  716.   }
  717.   *e1 = 0;
  718.   *e2 = 0;
  719. }
  720.  
  721. void Relations_Extract
  722. # ifdef __STDC__
  723. (Relations_tRelation *Rel, INTEGER *e1, INTEGER *e2)
  724. # else
  725. (Rel, e1, e2)
  726. Relations_tRelation *Rel;
  727. INTEGER *e1, *e2;
  728. # endif
  729. {
  730.   Relations_Select(Rel, e1, e2);
  731.   Relations_Exclude(Rel, *e1, *e2);
  732. }
  733.  
  734. static BOOLEAN gProc1b
  735. # ifdef __STDC__
  736. (CARDINAL e)
  737. # else
  738. (e)
  739. CARDINAL e;
  740. # endif
  741. {
  742.   return (*gProc2b)((LONGINT)i, (LONGINT)e);
  743. }
  744.  
  745. BOOLEAN Relations_Forall
  746. # ifdef __STDC__
  747. (Relations_tRelation Rel, Relations_ProcOfIntIntToBool Proc)
  748. # else
  749. (Rel, Proc)
  750. Relations_tRelation Rel;
  751. Relations_ProcOfIntIntToBool Proc;
  752. # endif
  753. {
  754.   gProc2b = Proc;
  755.   {
  756.     SHORTCARD B_47 = 0, B_48 = Rel.Size1;
  757.  
  758.     if (B_47 <= B_48)
  759.       for (i = B_47;; i += 1) {
  760.         if (!Sets_Forall(Rel.ArrayPtr->A[i], (Sets_ProcOfCardToBool)gProc1b)) {
  761.           return FALSE;
  762.         }
  763.         if (i >= B_48) break;
  764.       }
  765.   }
  766.   return TRUE;
  767. }
  768.  
  769. BOOLEAN Relations_Exists
  770. # ifdef __STDC__
  771. (Relations_tRelation Rel, Relations_ProcOfIntIntToBool Proc)
  772. # else
  773. (Rel, Proc)
  774. Relations_tRelation Rel;
  775. Relations_ProcOfIntIntToBool Proc;
  776. # endif
  777. {
  778.   gProc2b = Proc;
  779.   {
  780.     SHORTCARD B_49 = 0, B_50 = Rel.Size1;
  781.  
  782.     if (B_49 <= B_50)
  783.       for (i = B_49;; i += 1) {
  784.         if (Sets_Exists(Rel.ArrayPtr->A[i], (Sets_ProcOfCardToBool)gProc1b)) {
  785.           return TRUE;
  786.         }
  787.         if (i >= B_50) break;
  788.       }
  789.   }
  790.   return FALSE;
  791. }
  792.  
  793. BOOLEAN Relations_Exists1
  794. # ifdef __STDC__
  795. (Relations_tRelation Rel, Relations_ProcOfIntIntToBool Proc)
  796. # else
  797. (Rel, Proc)
  798. Relations_tRelation Rel;
  799. Relations_ProcOfIntIntToBool Proc;
  800. # endif
  801. {
  802.   INTEGER n;
  803.  
  804.   n = 0;
  805.   gProc2b = Proc;
  806.   {
  807.     SHORTCARD B_51 = 0, B_52 = Rel.Size1;
  808.  
  809.     if (B_51 <= B_52)
  810.       for (i = B_51;; i += 1) {
  811.         if (Sets_Exists(Rel.ArrayPtr->A[i], (Sets_ProcOfCardToBool)gProc1b)) {
  812.           INC(n);
  813.         }
  814.         if (i >= B_52) break;
  815.       }
  816.   }
  817.   return n == 1;
  818. }
  819.  
  820. static void gProc1
  821. # ifdef __STDC__
  822. (CARDINAL e)
  823. # else
  824. (e)
  825. CARDINAL e;
  826. # endif
  827. {
  828.   (*gProc2)((LONGINT)i, (LONGINT)e);
  829. }
  830.  
  831. void Relations_ForallDo
  832. # ifdef __STDC__
  833. (Relations_tRelation Rel, Relations_ProcOfIntInt Proc)
  834. # else
  835. (Rel, Proc)
  836. Relations_tRelation Rel;
  837. Relations_ProcOfIntInt Proc;
  838. # endif
  839. {
  840.   gProc2 = Proc;
  841.   {
  842.     SHORTCARD B_53 = 0, B_54 = Rel.Size1;
  843.  
  844.     if (B_53 <= B_54)
  845.       for (i = B_53;; i += 1) {
  846.         Sets_ForallDo(Rel.ArrayPtr->A[i], (Sets_ProcOfCard)gProc1);
  847.         if (i >= B_54) break;
  848.       }
  849.   }
  850. }
  851.  
  852. void Relations_ReadRelation
  853. # ifdef __STDC__
  854. (IO_tFile f, Relations_tRelation *Rel)
  855. # else
  856. (f, Rel)
  857. IO_tFile f;
  858. Relations_tRelation *Rel;
  859. # endif
  860. {
  861.   CHAR ch;
  862.  
  863.   do {
  864.   } while (!(IO_ReadC(f) == '{'));
  865.   Relations_AssignEmpty(Rel);
  866.   for (;;) {
  867.     if (IO_ReadC(f) == '}') {
  868.       goto EXIT_1;
  869.     }
  870.     i = IO_ReadI(f);
  871.     Relations_Include(Rel, (LONGINT)i, IO_ReadI(f));
  872.     ch = IO_ReadC(f);
  873.   } EXIT_1:;
  874. }
  875.  
  876. void Relations_WriteRelation
  877. # ifdef __STDC__
  878. (IO_tFile f, Relations_tRelation Rel)
  879. # else
  880. (f, Rel)
  881. IO_tFile f;
  882. Relations_tRelation Rel;
  883. # endif
  884. {
  885.   g = f;
  886.   IO_WriteC(f, '{');
  887.   Relations_ForallDo(Rel, (Relations_ProcOfIntInt)WritePair);
  888.   IO_WriteC(f, '}');
  889. }
  890.  
  891. static void WritePair
  892. # ifdef __STDC__
  893. (INTEGER e1, INTEGER e2)
  894. # else
  895. (e1, e2)
  896. INTEGER e1, e2;
  897. # endif
  898. {
  899.   IO_WriteC(g, ' ');
  900.   IO_WriteI(g, e1, 1L);
  901.   IO_WriteC(g, ' ');
  902.   IO_WriteI(g, e2, 1L);
  903.   IO_WriteC(g, ',');
  904. }
  905.  
  906. void BEGIN_Relations()
  907. {
  908.   static BOOLEAN has_been_called = FALSE;
  909.  
  910.   if (!has_been_called) {
  911.     has_been_called = TRUE;
  912.  
  913.     BEGIN_IO();
  914.     BEGIN_Sets();
  915.     BEGIN_IO();
  916.     BEGIN_DynArray();
  917.     BEGIN_Sets();
  918.     BEGIN_Sets();
  919.  
  920.   }
  921. }
  922.